package com.tanhua.server.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.SettingsService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class SettingsServiceImpl implements SettingsService {

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;
    /**
     * 查询当前用户通用设置
     * @return
     */
    @Override
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        //1.获取当前用户的id 和 手机号， 从token中获取
        Long userId = UserHolder.getUserId();
        String userMobile = UserHolder.getUserMobile();
        vo.setId(userId);
        vo.setPhone(userMobile);
        //2.获取陌生人问题
        Question question = questionApi.queryQuestion(userId);
        String questionTxt = question == null?"你喜欢我吗？": question.getTxt();//判断question中是否已经有问题，没有的话给一个默认问题
        vo.setStrangerQuestion(questionTxt);
        //3.获取通知设置
        Settings settings = settingsApi.querySettings(userId);
        if (settings!=null){
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
        }
        return vo;
    }

    /**
     * 修改或保存配置
     * @param settings
     */
    @Override
    public void setSettings(Settings settings) {
        Long userId = UserHolder.getUserId();
        Settings settings1 = settingsApi.querySettings(userId);
        if (settings1==null){
            settings.setUserId(userId);
            settingsApi.save(settings);
        }else {
            settings1.setGonggaoNotification(settings.getGonggaoNotification());
            settings1.setPinglunNotification(settings.getPinglunNotification());
            settings1.setLikeNotification(settings.getLikeNotification());
            settingsApi.update(settings1);
        }
    }

    /**
     * 分页查询黑名单
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult blackList(int page, int pageSize) {
        //获取当前用户ID
        Long userId = UserHolder.getUserId();
        //调用api查询用户黑名单分页列表
        IPage<UserInfo> iPage = blackListApi.queryByPage(userId, page, pageSize);

        //将返回的page对象封装到pageResult
        PageResult pageResult = new PageResult(page,pageSize,iPage.getTotal(),iPage.getRecords());

        //返回
        return pageResult;
    }
}
